<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .red{
            background-color: red;
        }
        .blue{
            background-color: blue;
        }
    </style>
</head>

<body>
    <div id="root">

    </div>
</body>
<script src="../react@17.js"></script>
<script src="../react-dom@17.js"></script>
<script src="../babel.js"></script>
<script type="text/babel">

    class App extends React.Component {
        state = {
            list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
            active:null
        }
        render() {
            let { list,active } = this.state;
            return (
                <div>
                    <ul>
                        {
                            list.map((item,index) => {
                                return (
                                    <li key={index} onClick={()=>{
                                        this.setState({
                                            active:index
                                        })
                                    }} style={this.getColor(index)}>{item}</li>
                                )
                            })
                        }
                    </ul>
                </div>
            )
        }
        getColor(index){
            let {active} = this.state;
           
            if(active ===null){
                return {}
            }
            if(active===index && index%2===0){
                return {
                    background:"red"
                }
            }
            if(active ===index && index%2!==0){
                return {
                     background:"blue"
                }
            }
        }
    }
    ReactDOM.render(<App/>,document.getElementById("root"))
</script>

</html>